package com.wtwd.campus.receiver;

import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.entity.MqSingleWhiteCallResult;
import com.wtwd.campus.service.IRMFeignClientService;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.LocationUtils;
import com.wtwd.campus.utils.UnicodeUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class SetSingleWhiteListNumberReceiver implements Receiver {
    @Autowired
    private IRMFeignClientService irmFeignClientService;

    @Override
    public String parseMessage(String data) {
        // *WT,866248050395985,V4,PBWL,095819,1,12487541256,1,0,\u0074\u0065\u0073\u0074,1,095821,A,2250.2256,N,11391.6190,E,0.18,162,030621,FFFFDFFF#
        String str = data.replace(",V4,",","); //防止消息携带V4指令导致解析失败
        //分割字符串获取指令信息
        String[] split = str.split(",");
        String imei = split[1];
        String cmdType = split[2];
        String commandNumber = split[3];
        Integer sequence = Integer.parseInt(split[4]);
        String phone = split[5];
        String type = split[6];
        String name = split[8];
        String wlMeid = split[7];
        String picId = split[9];
        String time = split[10];//时分秒
        String lat = split[12];
        String lng = split[14];
        String speed = split[16];
        String orientation = split[17];
        String date = split[18];//日月年
        String deviceStatus = split[19];//设备状态
        List<Double> list = LocationUtils.transiformLatAndLng(lng, lat);
        MqSingleWhiteCallResult mqSingleWhiteCallResult = new MqSingleWhiteCallResult();
        mqSingleWhiteCallResult.setCommandNumber(commandNumber);
        mqSingleWhiteCallResult.setImei(imei);
        mqSingleWhiteCallResult.setDeviceDate(date);
        mqSingleWhiteCallResult.setDeviceTime(time);
        mqSingleWhiteCallResult.setSequence(sequence);
        mqSingleWhiteCallResult.setName(StringUtils.isBlank(name) ? "" : UnicodeUtil.decodeUnicode(name));
        mqSingleWhiteCallResult.setPhone(phone);
        MqLocationData mqLocationData = new MqLocationData();
        mqLocationData.setOrientation(Integer.parseInt(orientation));
        mqLocationData.setSpeed(Double.parseDouble(speed));
        mqLocationData.setLatitude(list.get(1));
        mqLocationData.setLongitude(list.get(0));
        mqSingleWhiteCallResult.setMqLocationData(mqLocationData);
        InjectFeignService.getIrmFeignClientService().whiteCallResponse(mqSingleWhiteCallResult);
        return imei;

//        if (split.length <= 5) {
//            MqNormalData mqNormalData = new MqNormalData();
//            mqNormalData.setCommandNumber(commandNumber);
//            mqNormalData.setImei(imei);
//            MqSingleWhiteListNumberData mqSingleWhiteListNumberData = new MqSingleWhiteListNumberData();
//            mqSingleWhiteListNumberData.setMqNormalData(mqNormalData);
//            InjectFeignService.getIrmFeignClientService().setSingleWhiteListNumber(mqSingleWhiteListNumberData);
//            return imei;
//        }
    }
}
